package com.bytz.modules.cms.reseller.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.common.constant.CommonConstant;
import com.bytz.modules.cms.productLine.entity.ProductLineLevel;
import com.bytz.modules.cms.reseller.entity.ResellerLevel;
import com.bytz.modules.cms.reseller.entity.ResellerLevelChange;
import com.bytz.modules.cms.reseller.mapper.ResellerLevelMapper;
import com.bytz.modules.cms.reseller.model.ResellerLineInfo;
import com.bytz.modules.cms.reseller.model.ResellerModel;
import com.bytz.modules.cms.reseller.service.IResellerLevelChangeService;
import com.bytz.modules.cms.reseller.service.IResellerLevelService;
import com.bytz.modules.cms.user.constants.AuditBatchConstants;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 莱宝CMS—经销商管理—经销商/产品线等级关联表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-10
 */
@Service
public class ResellerLevelServiceImpl extends ServiceImpl<ResellerLevelMapper, ResellerLevel> implements IResellerLevelService {

    @Autowired
    private IResellerLevelChangeService resellerLevelChangeService;

    @Override
    public List<ResellerLevel> getByResellerId(String resellerId) {
        return lambdaQuery().eq(ResellerLevel::getResellerId, resellerId).list();
    }

    @Override
    public List<String> getLevelNameByResellerId(String resellerId) {
        MPJLambdaWrapper<ResellerLevel> wrapper = new MPJLambdaWrapper<>();
        return baseMapper.selectJoinList(String.class, wrapper
                .select(ProductLineLevel::getLevelName)
                .innerJoin(ProductLineLevel.class, ProductLineLevel::getId, ResellerLevel::getProductLineLevelId)
                .eq(ProductLineLevel::getDelFlag, CommonConstant.DEL_FLAG_0)
                .eq(ResellerLevel::getResellerId, resellerId)
                .orderByAsc(ProductLineLevel::getProductLineName));
    }

    @Override
    public void saveLevelByResellerModel(ResellerModel saveRo) {
        // 保存产品线等级
        Map<String, ResellerLineInfo> lineInfoMap = saveRo.getLineInfos().stream()
                .collect(Collectors.toMap(t -> t.getProductLine().getId(), t -> t));

        List<ResellerLevel> updateLevelList = getByResellerId(saveRo.getId()).stream().map(level -> {
            String newLevelId = lineInfoMap.get(level.getProductLineId()).getResellerLevel().getId();
            if (StringUtils.equals(newLevelId, level.getProductLineLevelId())) {
                return null;
            } else {
                level.setProductLineLevelId(newLevelId);
                return level;
            }
        }).filter(Objects::nonNull).collect(Collectors.toList());

        // 留经销商等级的change表记录
        List<ResellerLevelChange> changeList = updateLevelList.stream().map(resellerLevel -> {

            ResellerLineInfo lineInfo = lineInfoMap.get(resellerLevel.getProductLineId());

            ResellerLevelChange change = new ResellerLevelChange();
            change.setResellerId(saveRo.getId());
            change.setResellerName(saveRo.getResellerName());
            change.setProductLineId(resellerLevel.getProductLineId());
            change.setProductLineName(lineInfo.getProductLine().getProductLineName());
            change.setContactPerson(saveRo.getContactPerson());
            change.setPhone(saveRo.getPhone());
            change.setLevelId(resellerLevel.getProductLineLevelId());
            change.setProductLineLevelName(lineInfo.getResellerLevel().getLevelName());
            change.setResellerLevelId(resellerLevel.getId());
            change.setSyncStatus(AuditBatchConstants.SyncStatus.SYNCED);
            change.setEffectTime(LocalDateTime.now());

            return change;
        }).collect(Collectors.toList());

        updateBatchById(updateLevelList);
        resellerLevelChangeService.saveBatch(changeList);
    }
}
